import { observable, action, runInAction, configure } from 'mobx'
import { getTagList, deleteTag, addTag, editTag } from './TagServ'
import { message} from 'antd' 

configure({
	enforceActions: 'observed'
})


class Tag {
	@observable visible = false
	@observable tagList = []
	@observable categoryName = ''
	@observable id = ''
	@observable status = 'add'

	@action
	doGetTagList = () => {
		getTagList().then((res) => {
			runInAction(() => {
				this.tagList = res.data.list
			})
		}, (err) => {
			console.log(err)
		})
	}

	@action
	doDeletetag = (id) => {
		deleteTag({id: id}).then((res) => {
			this.doGetTagList()
		}, (err) => {
			console.log(err)
		})
	}

	@action
	doAddtag = (categoryName) => {
		addTag({categoryName: categoryName}).then((res) => {
			this.doGetTagList()
			runInAction(() => {
				this.visible = !this.visible
			})
			message.success('添加成功')
		}, (err) => {
			console.log(err)
		})
	}

	@action
	doEdittag = (id, categoryName) => {
		editTag({id: id, categoryName: categoryName}).then((res) => {
			this.doGetTagList()
			runInAction(() => {
				this.visible = !this.visible
			})
			message.success('编辑成功')
		}, (err) => {
			console.log(err)
		})
	}

}

export default Tag